package de.pernpas.calendar.servlet.event;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.pernpas.calendar.Navigator;
import de.pernpas.calendar.controller.Controller;
import de.pernpas.calendar.entity.Event;
import de.pernpas.calendar.service.EventService;

public class CreateEvent extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final Logger log = Logger.getLogger(Controller.class.getName());
	
	public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException{
		doPost(req, res);
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException{
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
		Event event = null;
		try {
			event = new Event(sdf.parse(req.getParameter("date")), 
					req.getParameter("location"), 
					req.getParameter("name"),
					req.getParameter("description"));
		} catch (ParseException e) {
			e.printStackTrace();
			log.warning(e.getMessage());
		}
		if(event != null){
			EventService.insertEvent(event);
		}
		
		res.sendRedirect(Navigator.getLink(req));
	}
}
